package com.cruise.game.controller.admin;

import com.cruise.game.common.api.ApiResult;
import com.cruise.game.controller.BaseController;
import com.cruise.game.model.vo.admin.AdminStatisticsVO;
import com.cruise.game.model.vo.admin.FinanceStatisticsVO;
import com.cruise.game.service.admin.AdminStatisticsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDate;

/**
 * 管理员统计控制器
 * 
 * @author cruise
 * @date 2025-02-28
 */
@Slf4j
@Api(tags = "管理员统计相关接口")
@RestController
@RequestMapping("/api/admin/statistics")
@PreAuthorize("hasRole('ROLE_ADMIN')")
public class AdminStatisticsController extends BaseController {

    @Resource
    private AdminStatisticsService adminStatisticsService;

    /**
     * 获取统计数据
     */
    @ApiOperation("获取统计数据")
    @GetMapping
    public ApiResult<AdminStatisticsVO> getStatistics() {
        return ApiResult.success(adminStatisticsService.getStatistics());
    }

    /**
     * 获取财务统计
     */
    @ApiOperation("获取财务统计")
    @GetMapping("/finance")
    public ApiResult<FinanceStatisticsVO> getFinanceStatistics(
            @ApiParam(value = "开始日期", required = false)
            @RequestParam(value = "startDate", required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            
            @ApiParam(value = "结束日期", required = false)
            @RequestParam(value = "endDate", required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        
        // 默认查询最近30天
        if (startDate == null) {
            startDate = LocalDate.now().minusDays(30);
        }
        if (endDate == null) {
            endDate = LocalDate.now();
        }
        
        return ApiResult.success(adminStatisticsService.getFinanceStatistics(startDate, endDate));
    }
}
